<template>
  <!-- 中国和省份地图组件 -->
  <div class="com-container">
    <div class="com-chart" ref="map"></div>
    <div class="iconfont icon-arrow-lift back-btn"
      :style="{fontSize:backFontSize+'px',color:theme==='chalk'?'#fff':'#000'}"
      v-show="showBack" @click="backChina"></div>
  </div>
</template>

<script>
import axios from 'axios'
import china from '../../public/static/map/china.json'
import { getProvinceMapInfo } from '../utils/map_utils'
import { mapState } from 'vuex'
export default {
  data() {
    return {
      // echarts对象
      myChart: null,
      // 数据
      mapData: [],
      // 省份地图
      province: {},
      // 返回按钮字体大小
      backFontSize: 0,
      // 显示返回箭头按钮
      showBack: false
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      this.myChart.dispose()
      this.initChart()
      this.updateChart()
      this.screenAdapter()
    }
  },
  mounted() {
    this.initChart()
    this.getData()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
  },
  destroyed() {
    window.removeEventListener('resize', this.screenAdapter)
    this.myChart.off('click')
  },
  methods: {
    // 初始化echarts对象
    initChart() {
      this.myChart = this.$echarts.init(this.$refs.map, this.theme)
      // 注册地图
      this.$echarts.registerMap('china', china)
      const initOption = {
        title: {
          text: '▎商家分布',
          top: 20,
          left: 20
        },
        geo: {
          type: 'map',
          map: 'china',
          top: '5%',
          bottom: '5%',
          itemStyle: {
            areaColor: '#2e72bf',
            borderColor: '#333'
          }
        },
        legend: {
          left: '5%',
          bottom: '5%',
          orient: 'vertical'
        }
      }
      this.myChart.setOption(initOption)
      this.myChart.on('click', async arg => {
        // 点击省份，获取省份地图数据
        const provinceInfo = getProvinceMapInfo(arg.name)
        if (provinceInfo === null) return
        // 避免重复请求同一省份数据
        if (!this.province[provinceInfo.key]) {
          const res = await axios.get(
            'http://localhost:8086' + provinceInfo.path
          )
          // 缓存省份地图数据
          this.province[provinceInfo.key] = provinceInfo.key
          this.$echarts.registerMap(provinceInfo.key, res.data)
        }
        const changeOption = {
          geo: {
            map: provinceInfo.key
          }
        }
        this.myChart.setOption(changeOption)
        this.showBack = true
      })
    },
    // 获取数据
    async getData() {
      const res = (this.mapData = await this.$getApi.getMap())
      this.updateChart()
    },
    // 更新图表
    updateChart() {
      const legendArr = []
      const seriesArr = []
      // 如果想在地图中显示散点的数据，所以我们需要给散点的图表增加一个配置,coordinateSystem : 'geo'
      this.mapData.forEach(item => {
        legendArr.push(item.name)
        seriesArr.push({
          type: 'effectScatter',
          name: item.name,
          data: item.children,
          coordinateSystem: 'geo',
          rippleEffect: {
            scale: 5,
            period: 3,
            brushType: 'stroke'
          }
        })
      })
      const dataOption = {
        legend: {
          data: legendArr
        },
        series: seriesArr
      }
      this.myChart.setOption(dataOption)
    },
    // 图表适配浏览器窗口大小变化
    screenAdapter() {
      const titleFontSize = (this.$refs.map.offsetWidth / 100) * 3.6
      this.backFontSize = titleFontSize
      const adaptOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        legend: {
          itemWidth: titleFontSize / 2,
          itemHeight: titleFontSize / 2,
          itemGap: titleFontSize / 2,
          textStyle: {
            fontSize: titleFontSize / 2
          }
        }
      }
      this.myChart.setOption(adaptOption)
      this.myChart.resize()
    },
    // 从省份地图返回中国地图
    backChina() {
      const changeOption = {
        geo: {
          map: 'china'
        }
      }
      this.myChart.setOption(changeOption)
      this.showBack = false
    }
  }
}
</script>

<style scoped>
.back-btn {
  position: absolute;
  top: 16px;
  right: 45%;
  color: #fff;
  cursor: pointer;
}
</style>
